package com.chapter17;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FutureValueIncPanel extends JPanel
  {
   private static final long serialVersionUID = 1;

   private JTextField  paymentTextField,
                       rateTextField,
                       yearsTextField,
                       futureValueTextField;
   private JLabel      paymentLabel,
                       rateLabel,
                       yearsLabel,
                       futureValueLabel;
   private JButton     calculateButton,
                       exitButton;

   public FutureValueIncPanel ()
     {
      // display panel
      JPanel displayPanel = new JPanel();
      displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

      // payment label
      paymentLabel = new JLabel("Monthly Payment:");
      displayPanel.add(paymentLabel);

      // payment text field
      paymentTextField = new JTextField(10);
      displayPanel.add(paymentTextField);

      // rate label
      rateLabel = new JLabel("Yearly Interest Rate:");
      displayPanel.add(rateLabel);

      // rate text field
      rateTextField = new JTextField(10);
      displayPanel.add(rateTextField);

      // years label
      yearsLabel = new JLabel("Number of Years:");
      displayPanel.add(yearsLabel);
      
      // years text field
      yearsTextField = new JTextField(10);
      displayPanel.add(yearsTextField);

      // future value label
      futureValueLabel = new JLabel("Future Value:");
      displayPanel.add(futureValueLabel);

      // future value text field
      futureValueTextField = new JTextField(10);
      futureValueTextField.setEditable(false);
      futureValueTextField.setFocusable(false);
      displayPanel.add(futureValueTextField);

      // button panel
      JPanel buttonPanel = new JPanel();
      buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

      // Set up action listener
      ActionListener listener = new FutureValueActionEvents();
      
      // calculate button
      calculateButton = new JButton("Calculate");
      calculateButton.addActionListener(listener);
      buttonPanel.add(calculateButton);

      // exit button
      exitButton = new JButton("Exit");
      exitButton.addActionListener(listener);
      buttonPanel.add(exitButton);

      // add panels to main panel
      this.setLayout(new BorderLayout());
      this.add(displayPanel, BorderLayout.CENTER);
      this.add(buttonPanel, BorderLayout.SOUTH);
     }

   class FutureValueActionEvents implements ActionListener
     {
      public void actionPerformed (ActionEvent e)
        {
         Object source = e.getSource();
         if (source == exitButton)
           System.exit(0);
         else
       	{
       	 if (source == calculateButton)
              {
               double       payment     = Double.parseDouble(paymentTextField.getText());
               double       rate        = Double.parseDouble(rateTextField.getText());
               int          years       = Integer.parseInt(yearsTextField.getText());
               double       futureValue = FinancialCalculations.calculateFutureValue(payment, rate, years);
               NumberFormat currency    = NumberFormat.getCurrencyInstance();
               futureValueTextField.setText(currency.format(futureValue));
              }
           }
        }
     }
  }
